import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class StatService extends BaseService<any> {
  private readonly logger = new Logger(StatService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * getIndexData
   * 对应 PHP: StatService_admin::getIndexData()
   * 逻辑类型: undefined - undefined
   */
  async getIndexData() {
    // 基于PHP真实逻辑: getIndexData
    // PHP原文: $data = [             'today_data' => [                 'member_count' => 1,                 'site_count' => 2,                 'visit_count' => 675, ...
const data = [
            today_data: [
                member_count: 1,
                site_count: 2,
                visit_count: 675,
                total_member_count: 0,
                total_site_count: 0,
                total_visit_count: 6840,
            ],
            system: [],
            version: [],
//            visit_stat: [
//                date: [],
//                value: [980, 1323, 882, 762, 865, 923, 1105]
//            ],
            site_stat: [
                date: [],
                value: []
            ],
            member_stat: [
                type: [ '男', '女', '未知' ],
                value: []
            ],
            site_group_stat: [
                type: [],
                value: []
            ],
            about: [
                [
                    name: 'Niucloud官方公众号',
                    image: 'static/resource/icon/index_icon/wx_qrcode.jpg',
                    desc: '微信扫码关注'
                ],
                [
                    name: '添加企业微信群',
                    image: 'static/resource/icon/index_icon/wework_qrcode.png',
                    desc: '更多内容请扫码加入'
                ]
            ],
            member_count_stat: [
                date: [],
                value: []
            ],
        ];

        const day_start_time = strtotime(date('Y-m-d')];
        //当天结束之间
        const day_end_time = day_start_time + 86400;
        data[ 'today_data' ][ 'total_member_count' ] = ( this.coreMemberService ).getCount(];
        data[ 'today_data' ][ 'today_member_count' ) = ( this.coreMemberService ).getCount([ create_time: get_start_and_end_time_by_day() ]];
        data[ 'today_data' ][ 'total_site_count' ] = ( this.siteService ).getCount(];
        data[ 'today_data' ][ 'today_site_count' ) = ( this.siteService ).getCount([ create_time: [ day_start_time, day_end_time ) )];
        data[ 'today_data' ][ 'norma_site_count' ] = ( this.siteService ).getCount([ status: [ 1 ], app_type: [ 'site' ) )];
        data[ 'today_data' ][ 'expire_site_count' ] = ( this.siteService ).getCount([ status: [ 2 ) )];
        data[ 'today_data' ][ 'week_expire_site_count' ] = ( this.siteService ).where([
            [ 'status', '=', 1 ),
            [ 'expire_time', 'BETWEEN', [ time(), time() + 86400 * 7 ] ]
        ]).count(];

        data[ 'system' ) = ( this.systemService ).getInfo(];
        data[ 'version' ] = data[ 'system' ][ 'version' ] || [];
        const time = time();
        for (i = 1; i <= 7; i++) {
            const item_day = date('Y-m-d', strtotime('+' . i - 7  + ' days', time)];
            data[ 'site_stat' ][ 'date' ][] = item_day;
            data[ 'site_stat' ][ 'value' ][] = ( this.siteService ).where([ [ 'create_time', 'between', get_start_and_end_time_by_day(item_day) ] ]).count(];

            data[ 'member_count_stat' ][ 'date' ][] = item_day;
            data[ 'member_count_stat' ][ 'value' ][) = ( this.memberService ).where([ [ 'create_time', 'between', get_start_and_end_time_by_day(item_day) ] ]).count(];
  }
}
